package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.ChangePhoneView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/19 0019.
 */

public class ChangePhonePerenster extends BasePresent<ChangePhoneView> {

    public void getIsAccountExists(String phone) {

        JSONObject object = new JSONObject();
        try {
            object.put("account", phone);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final String requestStr = object.toString();
        final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);
        RetrofitSerives requestSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddress);
        Call<String> accountCall = requestSerives.getIsAccountExists(body);

        accountCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    view.isAccountExists(response.body());
                } else {
                    view.onError("请求失败，请重试");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                view.onError("请求失败，请重试");
            }
        });
    }

    /**
     * 获取短信验证码
     *
     * @param phone
     */
    public void getMesCode(String phone) {

        JSONObject object = new JSONObject();
        try {
            object.put("phone", phone);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final String requestStr = object.toString();

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);

        RetrofitSerives requestSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        Call<String> call = requestSerives.getMesvcode(body);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    view.setVcodeResult(response.body());
                } else {
                    view.onError("请求失败，请重试");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                view.onError("请求服务器异常，稍候再试" + t.getMessage());
            }
        });
    }

    public void getChangePhoneResult(int id, String vcode, String phone, int flag){

        JSONObject object = new JSONObject();

        try {
            object.put("sellerId", id);
            object.put("vcode", vcode);
            object.put("phone", phone);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final String requestStr = object.toString();

        final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);

        RetrofitSerives requestSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        Call<ResultMessage> call = requestSerives.getChangeBindPhone2(body);

        call.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {
                    view.setChangeBindPhone(response.body());
                } else {
                    view.onError("请求失败，请重试");
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError("请求服务器异常，稍候再试" + t.getMessage());
            }
        });

    }

    @Override
    public void stopRequest() {

    }
}
